





<!DOCTYPE html>
<html class="writer-html5" lang="zh-CN" >
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>Deploy Pretrained Vision Detection Model from Darknet on VTA &mdash; tvm 0.8.dev1982 文档</title>
  

  
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/gallery.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../../_static/css/tlcpack_theme.css" type="text/css" />

  
  
    <link rel="shortcut icon" href="../../../../_static/tvm-logo-square.png"/>
  

  
  
  
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../../" src="../../../../_static/documentation_options.js"></script>
        <script data-url_root="../../../../" id="documentation_options" src="../../../../_static/documentation_options.js"></script>
        <script src="../../../../_static/jquery.js"></script>
        <script src="../../../../_static/underscore.js"></script>
        <script src="../../../../_static/doctools.js"></script>
        <script src="../../../../_static/translations.js"></script>
    
    <script type="text/javascript" src="../../../../_static/js/theme.js"></script>

    
    <script type="text/javascript" src="../../../../_static/js/tlcpack_theme.js"></script>
    <link rel="index" title="索引" href="../../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../../search.html" />
    <link rel="next" title="2D Convolution Optimization" href="../optimize/convolution_opt.html" />
    <link rel="prev" title="Deploy Pretrained Vision Model from MxNet on VTA" href="deploy_classification.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    
<header class="header">
    <div class="innercontainer">
      <div class="headerInner d-flex justify-content-between align-items-center">
          <div class="headerLogo">
               <a href="https://tvm.apache.org/"><img src=https://tvm.apache.org/assets/images/logo.svg alt="logo"></a>
          </div>

          <div id="headMenu" class="headerNav">
            <button type="button" id="closeHeadMenu" class="navCloseBtn"><img src="../../../../_static/img/close-icon.svg" alt="Close"></button>
             <ul class="nav">
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/community>Community</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/download>Download</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/vta>VTA</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/blog>Blog</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvm.apache.org/docs>Docs</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvmconf.org>Conference</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://github.com/apache/tvm/>Github</a>
                </li>
                <li class="nav-item">
                   <a class="nav-link" href=https://tvmchinese.github.io/declaration_zh_CN.html>About-Translators</a>
                </li>
             </ul>
               <div class="responsivetlcdropdown">
                 <button type="button" class="btn-link">
                   ASF
                 </button>
                 <ul>
                     <li>
                       <a href=https://apache.org/>Apache Homepage</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/licenses/>License</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/foundation/sponsorship.html>Sponsorship</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/security/>Security</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/foundation/thanks.html>Thanks</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/events/current-event>Events</a>
                     </li>
                     <li>
                       <a href=https://www.zhihu.com/column/c_1429578595417563136>Zhihu</a>
                     </li>
                 </ul>
               </div>
          </div>
            <div class="responsiveMenuIcon">
              <button type="button" id="menuBtn" class="btn-menu"><img src="../../../../_static/img/menu-icon.svg" alt="Menu Icon"></button>
            </div>

            <div class="tlcDropdown">
              <div class="dropdown">
                <button type="button" class="btn-link dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  ASF
                </button>
                <div class="dropdown-menu dropdown-menu-right">
                  <ul>
                     <li>
                       <a href=https://apache.org/>Apache Homepage</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/licenses/>License</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/foundation/sponsorship.html>Sponsorship</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/security/>Security</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/foundation/thanks.html>Thanks</a>
                     </li>
                     <li>
                       <a href=https://www.apache.org/events/current-event>Events</a>
                     </li>
                     <li>
                       <a href=https://www.zhihu.com/column/c_1429578595417563136>Zhihu</a>
                     </li>
                  </ul>
                </div>
              </div>
          </div>
       </div>
    </div>
 </header>
 
    <nav data-toggle="wy-nav-shift" class="wy-nav-side fixed">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../../index.html">
          

          
            
            <img src="../../../../_static/tvm-logo-small.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
                <div class="version">
                  0.8.dev1982
                </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption" role="heading"><span class="caption-text">如何开始</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../install/index.html">安装 TVM</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../contribute/index.html">贡献者指南</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">用户引导</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../tutorial/index.html">User Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../how_to/index.html">How To Guides</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">开发者引导</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../dev/tutorial/index.html">Developer Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../dev/how_to/how_to.html">开发者指南</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">架构指南</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../arch/index.html">Design and Architecture</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">主题引导</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../../../microtvm/index.html">microTVM：裸机使用TVM</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../../index.html">VTA: Versatile Tensor Accelerator</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../../install.html">VTA安装指南</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../dev/index.html">VTA设计与开发指南</a></li>
<li class="toctree-l2 current"><a class="reference internal" href="../index.html">VTA教程</a><ul class="current">
<li class="toctree-l3"><a class="reference internal" href="../matrix_multiply.html">Simple Matrix Multiply</a></li>
<li class="toctree-l3"><a class="reference internal" href="../vta_get_started.html">从 VTA 开始</a></li>
<li class="toctree-l3 current"><a class="reference internal" href="../index.html#compile-deep-learning-models">编译深度学习模型</a><ul class="current">
<li class="toctree-l4"><a class="reference internal" href="deploy_classification.html">Deploy Pretrained Vision Model from MxNet on VTA</a></li>
<li class="toctree-l4 current"><a class="current reference internal" href="#">Deploy Pretrained Vision Detection Model from Darknet on VTA</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="../index.html#optimize-tensor-operators">优化张量算子</a></li>
<li class="toctree-l3"><a class="reference internal" href="../index.html#auto-tuning">自动调整</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../index.html#literature">Literature</a></li>
</ul>
</li>
</ul>
<p class="caption" role="heading"><span class="caption-text">参考指南</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../reference/langref/index.html">语言参考</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../reference/api/python/index.html">Python API</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../reference/api/links.html">Other APIs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../reference/publications.html">Publications</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../genindex.html">索引</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
      
      <nav class="wy-nav-top" aria-label="top navigation" data-toggle="wy-nav-top">
        
            <div class="togglemenu">

            </div>
            <div class="nav-content">
              <!-- tvm -->
              Table of content
            </div>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        

          




















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../index.html">Docs</a> <span class="br-arrow">></span></li>
        
          <li><a href="../../index.html">VTA: Versatile Tensor Accelerator</a> <span class="br-arrow">></span></li>
        
          <li><a href="../index.html">VTA教程</a> <span class="br-arrow">></span></li>
        
      <li>Deploy Pretrained Vision Detection Model from Darknet on VTA</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
            <a href="../../../../_sources/topic/vta/tutorials/frontend/deploy_detection.rst.txt" rel="nofollow"> <img src="../../../../_static//img/source.svg" alt="viewsource"/></a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="sphx-glr-download-link-note admonition note">
<p class="admonition-title">注解</p>
<p>点击 <a class="reference internal" href="#sphx-glr-download-topic-vta-tutorials-frontend-deploy-detection-py"><span class="std std-ref">此处</span></a> 获取完整示例代码</p>
</div>
<div class="sphx-glr-example-title section" id="deploy-pretrained-vision-detection-model-from-darknet-on-vta">
<span id="sphx-glr-topic-vta-tutorials-frontend-deploy-detection-py"></span><h1>Deploy Pretrained Vision Detection Model from Darknet on VTA<a class="headerlink" href="#deploy-pretrained-vision-detection-model-from-darknet-on-vta" title="永久链接至标题">¶</a></h1>
<p><strong>作者</strong>: <a class="reference external" href="https://github.com/huajsj">Hua Jiang</a></p>
<p>This tutorial provides an end-to-end demo, on how to run Darknet YoloV3-tiny
inference onto the VTA accelerator design to perform Image detection tasks.
It showcases Relay as a front end compiler that can perform quantization (VTA
only supports int8/32 inference) as well as graph packing (in order to enable
tensorization in the core) to massage the compute graph for the hardware target.</p>
<div class="section" id="install-dependencies">
<h2>安装依赖<a class="headerlink" href="#install-dependencies" title="永久链接至标题">¶</a></h2>
<p>To use the autotvm package in tvm, we need to install some extra dependencies.
(change “3” to “2” if you use python2):</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>
</pre></div>
</div>
<p>pip3 install “Pillow&lt;7”</p>
<p>YOLO-V3-tiny Model with Darknet parsing have dependancy with CFFI and CV2 library,
we need to install CFFI and CV2 before executing this script.</p>
<p>pip3 install “Pillow&lt;7”</p>
<p>pip3 install cffi
pip3 install opencv-python</p>
<p>Now return to the python code. Import packages.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">absolute_import</span><span class="p">,</span> <span class="n">print_function</span>

<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">tvm</span>
<span class="kn">import</span> <span class="nn">vta</span>
<span class="kn">from</span> <span class="nn">tvm</span> <span class="k">import</span> <span class="n">rpc</span><span class="p">,</span> <span class="n">autotvm</span><span class="p">,</span> <span class="n">relay</span>
<span class="kn">from</span> <span class="nn">tvm.relay.testing</span> <span class="k">import</span> <span class="n">yolo_detection</span><span class="p">,</span> <span class="n">darknet</span>
<span class="kn">from</span> <span class="nn">tvm.relay.testing.darknet</span> <span class="k">import</span> <span class="n">__darknetffi__</span>
<span class="kn">from</span> <span class="nn">tvm.contrib</span> <span class="k">import</span> <span class="n">graph_executor</span><span class="p">,</span> <span class="n">utils</span>
<span class="kn">from</span> <span class="nn">tvm.contrib.download</span> <span class="k">import</span> <span class="n">download_testdata</span>
<span class="kn">from</span> <span class="nn">vta.testing</span> <span class="k">import</span> <span class="n">simulator</span>
<span class="kn">from</span> <span class="nn">vta.top</span> <span class="k">import</span> <span class="n">graph_pack</span>

<span class="c1"># Make sure that TVM was compiled with RPC=1</span>
<span class="k">assert</span> <span class="n">tvm</span><span class="o">.</span><span class="n">runtime</span><span class="o">.</span><span class="n">enabled</span><span class="p">(</span><span class="s2">&quot;rpc&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>Download yolo net configure file, weight file, darknet library file based on
Model Name
—————————————————————————-</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">MODEL_NAME</span> <span class="o">=</span> <span class="s2">&quot;yolov3-tiny&quot;</span>
<span class="n">REPO_URL</span> <span class="o">=</span> <span class="s2">&quot;https://github.com/dmlc/web-data/blob/main/darknet/&quot;</span>

<span class="n">cfg_path</span> <span class="o">=</span> <span class="n">download_testdata</span><span class="p">(</span>
    <span class="s2">&quot;https://github.com/pjreddie/darknet/blob/master/cfg/&quot;</span> <span class="o">+</span> <span class="n">MODEL_NAME</span> <span class="o">+</span> <span class="s2">&quot;.cfg&quot;</span> <span class="o">+</span> <span class="s2">&quot;?raw=true&quot;</span><span class="p">,</span>
    <span class="n">MODEL_NAME</span> <span class="o">+</span> <span class="s2">&quot;.cfg&quot;</span><span class="p">,</span>
    <span class="n">module</span><span class="o">=</span><span class="s2">&quot;darknet&quot;</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">weights_path</span> <span class="o">=</span> <span class="n">download_testdata</span><span class="p">(</span>
    <span class="s2">&quot;https://pjreddie.com/media/files/&quot;</span> <span class="o">+</span> <span class="n">MODEL_NAME</span> <span class="o">+</span> <span class="s2">&quot;.weights&quot;</span> <span class="o">+</span> <span class="s2">&quot;?raw=true&quot;</span><span class="p">,</span>
    <span class="n">MODEL_NAME</span> <span class="o">+</span> <span class="s2">&quot;.weights&quot;</span><span class="p">,</span>
    <span class="n">module</span><span class="o">=</span><span class="s2">&quot;darknet&quot;</span><span class="p">,</span>
<span class="p">)</span>

<span class="k">if</span> <span class="n">sys</span><span class="o">.</span><span class="n">platform</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;linux&quot;</span><span class="p">,</span> <span class="s2">&quot;linux2&quot;</span><span class="p">]:</span>
    <span class="n">darknet_lib_path</span> <span class="o">=</span> <span class="n">download_testdata</span><span class="p">(</span>
        <span class="n">REPO_URL</span> <span class="o">+</span> <span class="s2">&quot;lib/&quot;</span> <span class="o">+</span> <span class="s2">&quot;libdarknet2.0.so&quot;</span> <span class="o">+</span> <span class="s2">&quot;?raw=true&quot;</span><span class="p">,</span> <span class="s2">&quot;libdarknet2.0.so&quot;</span><span class="p">,</span> <span class="n">module</span><span class="o">=</span><span class="s2">&quot;darknet&quot;</span>
    <span class="p">)</span>
<span class="k">elif</span> <span class="n">sys</span><span class="o">.</span><span class="n">platform</span> <span class="o">==</span> <span class="s2">&quot;darwin&quot;</span><span class="p">:</span>
    <span class="n">darknet_lib_path</span> <span class="o">=</span> <span class="n">download_testdata</span><span class="p">(</span>
        <span class="n">REPO_URL</span> <span class="o">+</span> <span class="s2">&quot;lib_osx/&quot;</span> <span class="o">+</span> <span class="s2">&quot;libdarknet_mac2.0.so&quot;</span> <span class="o">+</span> <span class="s2">&quot;?raw=true&quot;</span><span class="p">,</span>
        <span class="s2">&quot;libdarknet_mac2.0.so&quot;</span><span class="p">,</span>
        <span class="n">module</span><span class="o">=</span><span class="s2">&quot;darknet&quot;</span><span class="p">,</span>
    <span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
    <span class="k">raise</span> <span class="ne">NotImplementedError</span><span class="p">(</span><span class="s2">&quot;Darknet lib is not supported on </span><span class="si">{}</span><span class="s2"> platform&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">platform</span><span class="p">))</span>
</pre></div>
</div>
</div>
<div class="section" id="download-yolo-categories-and-illustration-front">
<h2>Download yolo categories and illustration front.<a class="headerlink" href="#download-yolo-categories-and-illustration-front" title="永久链接至标题">¶</a></h2>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">coco_path</span> <span class="o">=</span> <span class="n">download_testdata</span><span class="p">(</span>
    <span class="n">REPO_URL</span> <span class="o">+</span> <span class="s2">&quot;data/&quot;</span> <span class="o">+</span> <span class="s2">&quot;coco.names&quot;</span> <span class="o">+</span> <span class="s2">&quot;?raw=true&quot;</span><span class="p">,</span> <span class="s2">&quot;coco.names&quot;</span><span class="p">,</span> <span class="n">module</span><span class="o">=</span><span class="s2">&quot;data&quot;</span>
<span class="p">)</span>
<span class="n">font_path</span> <span class="o">=</span> <span class="n">download_testdata</span><span class="p">(</span>
    <span class="n">REPO_URL</span> <span class="o">+</span> <span class="s2">&quot;data/&quot;</span> <span class="o">+</span> <span class="s2">&quot;arial.ttf&quot;</span> <span class="o">+</span> <span class="s2">&quot;?raw=true&quot;</span><span class="p">,</span> <span class="s2">&quot;arial.ttf&quot;</span><span class="p">,</span> <span class="n">module</span><span class="o">=</span><span class="s2">&quot;data&quot;</span>
<span class="p">)</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">coco_path</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="n">content</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">readlines</span><span class="p">()</span>
<span class="n">names</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">content</span><span class="p">]</span>
</pre></div>
</div>
</div>
<div class="section" id="define-the-platform-and-model-targets">
<h2>Define the platform and model targets.<a class="headerlink" href="#define-the-platform-and-model-targets" title="永久链接至标题">¶</a></h2>
<p>Execute on CPU vs. VTA, and define the model.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Load VTA parameters from the 3rdparty/vta-hw/config/vta_config.json file</span>
<span class="n">env</span> <span class="o">=</span> <span class="n">vta</span><span class="o">.</span><span class="n">get_env</span><span class="p">()</span>
<span class="c1"># Set ``device=arm_cpu`` to run inference on the CPU</span>
<span class="c1"># or ``device=vta`` to run inference on the FPGA.</span>
<span class="n">device</span> <span class="o">=</span> <span class="s2">&quot;vta&quot;</span>
<span class="n">target</span> <span class="o">=</span> <span class="n">env</span><span class="o">.</span><span class="n">target</span> <span class="k">if</span> <span class="n">device</span> <span class="o">==</span> <span class="s2">&quot;vta&quot;</span> <span class="k">else</span> <span class="n">env</span><span class="o">.</span><span class="n">target_vta_cpu</span>

<span class="n">pack_dict</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s2">&quot;yolov3-tiny&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;nn.max_pool2d&quot;</span><span class="p">,</span> <span class="s2">&quot;cast&quot;</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">186</span><span class="p">],</span>
<span class="p">}</span>

<span class="c1"># Name of Darknet model to compile</span>
<span class="c1"># The ``start_pack`` and ``stop_pack`` labels indicate where</span>
<span class="c1"># to start and end the graph packing relay pass: in other words</span>
<span class="c1"># where to start and finish offloading to VTA.</span>
<span class="c1"># the number 4 indicate the the ``start_pack`` index is 4, the</span>
<span class="c1"># number 186 indicate the ``stop_pack index`` is 186, by using</span>
<span class="c1"># name and index number, here we can located to correct place</span>
<span class="c1"># where to start/end when there are multiple ``nn.max_pool2d``</span>
<span class="c1"># or ``cast``, print(mod.astext(show_meta_data=False)) can help</span>
<span class="c1"># to find operator name and index information.</span>
<span class="k">assert</span> <span class="n">MODEL_NAME</span> <span class="ow">in</span> <span class="n">pack_dict</span>
</pre></div>
</div>
</div>
<div class="section" id="obtain-an-execution-remote">
<h2>Obtain an execution remote.<a class="headerlink" href="#obtain-an-execution-remote" title="永久链接至标题">¶</a></h2>
<p>When target is ‘pynq’ or other FPGA backend, reconfigure FPGA and runtime.
Otherwise, if target is ‘sim’, execute locally.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">env</span><span class="o">.</span><span class="n">TARGET</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;sim&quot;</span><span class="p">,</span> <span class="s2">&quot;tsim&quot;</span><span class="p">]:</span>
    <span class="c1"># Get remote from tracker node if environment variable is set.</span>
    <span class="c1"># To set up the tracker, you&#39;ll need to follow the &quot;Auto-tuning</span>
    <span class="c1"># a convolutional network for VTA&quot; tutorial.</span>
    <span class="n">tracker_host</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;TVM_TRACKER_HOST&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
    <span class="n">tracker_port</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;TVM_TRACKER_PORT&quot;</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
    <span class="c1"># Otherwise if you have a device you want to program directly from</span>
    <span class="c1"># the host, make sure you&#39;ve set the variables below to the IP of</span>
    <span class="c1"># your board.</span>
    <span class="n">device_host</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;VTA_RPC_HOST&quot;</span><span class="p">,</span> <span class="s2">&quot;192.168.2.99&quot;</span><span class="p">)</span>
    <span class="n">device_port</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">environ</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;VTA_RPC_PORT&quot;</span><span class="p">,</span> <span class="s2">&quot;9091&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">tracker_host</span> <span class="ow">or</span> <span class="ow">not</span> <span class="n">tracker_port</span><span class="p">:</span>
        <span class="n">remote</span> <span class="o">=</span> <span class="n">rpc</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">device_host</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">device_port</span><span class="p">))</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">remote</span> <span class="o">=</span> <span class="n">autotvm</span><span class="o">.</span><span class="n">measure</span><span class="o">.</span><span class="n">request_remote</span><span class="p">(</span>
            <span class="n">env</span><span class="o">.</span><span class="n">TARGET</span><span class="p">,</span> <span class="n">tracker_host</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">tracker_port</span><span class="p">),</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">10000</span>
        <span class="p">)</span>
    <span class="c1"># Reconfigure the JIT runtime and FPGA.</span>
    <span class="c1"># You can program the FPGA with your own custom bitstream</span>
    <span class="c1"># by passing the path to the bitstream file instead of None.</span>
    <span class="n">reconfig_start</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>
    <span class="n">vta</span><span class="o">.</span><span class="n">reconfig_runtime</span><span class="p">(</span><span class="n">remote</span><span class="p">)</span>
    <span class="n">vta</span><span class="o">.</span><span class="n">program_fpga</span><span class="p">(</span><span class="n">remote</span><span class="p">,</span> <span class="n">bitstream</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
    <span class="n">reconfig_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="n">reconfig_start</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Reconfigured FPGA and RPC runtime in </span><span class="si">{0:.2f}</span><span class="s2">s!&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">reconfig_time</span><span class="p">))</span>

<span class="c1"># In simulation mode, host the RPC server locally.</span>
<span class="k">else</span><span class="p">:</span>
    <span class="n">remote</span> <span class="o">=</span> <span class="n">rpc</span><span class="o">.</span><span class="n">LocalSession</span><span class="p">()</span>

<span class="c1"># Get execution context from remote</span>
<span class="n">ctx</span> <span class="o">=</span> <span class="n">remote</span><span class="o">.</span><span class="n">ext_dev</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="k">if</span> <span class="n">device</span> <span class="o">==</span> <span class="s2">&quot;vta&quot;</span> <span class="k">else</span> <span class="n">remote</span><span class="o">.</span><span class="n">cpu</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="build-the-inference-graph-executor">
<h2>Build the inference graph executor.<a class="headerlink" href="#build-the-inference-graph-executor" title="永久链接至标题">¶</a></h2>
<p>Using Darknet library load downloaded vision model and compile with Relay.
The compilation steps are:</p>
<ol class="arabic simple">
<li><p>Front end translation from Darknet into Relay module.</p></li>
<li><p>Apply 8-bit quantization: here we skip the first conv layer,
and dense layer which will both be executed in fp32 on the CPU.</p></li>
<li><p>Perform graph packing to alter the data layout for tensorization.</p></li>
<li><p>Perform constant folding to reduce number of operators (e.g. eliminate batch norm multiply).</p></li>
<li><p>Perform relay build to object file.</p></li>
<li><p>Load the object file onto remote (FPGA device).</p></li>
<li><p>Generate graph executor, <cite>m</cite>.</p></li>
</ol>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Load pre-configured AutoTVM schedules</span>
<span class="k">with</span> <span class="n">autotvm</span><span class="o">.</span><span class="n">tophub</span><span class="o">.</span><span class="n">context</span><span class="p">(</span><span class="n">target</span><span class="p">):</span>
    <span class="n">net</span> <span class="o">=</span> <span class="n">__darknetffi__</span><span class="o">.</span><span class="n">dlopen</span><span class="p">(</span><span class="n">darknet_lib_path</span><span class="p">)</span><span class="o">.</span><span class="n">load_network</span><span class="p">(</span>
        <span class="n">cfg_path</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s2">&quot;utf-8&quot;</span><span class="p">),</span> <span class="n">weights_path</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s2">&quot;utf-8&quot;</span><span class="p">),</span> <span class="mi">0</span>
    <span class="p">)</span>
    <span class="n">dshape</span> <span class="o">=</span> <span class="p">(</span><span class="n">env</span><span class="o">.</span><span class="n">BATCH</span><span class="p">,</span> <span class="n">net</span><span class="o">.</span><span class="n">c</span><span class="p">,</span> <span class="n">net</span><span class="o">.</span><span class="n">h</span><span class="p">,</span> <span class="n">net</span><span class="o">.</span><span class="n">w</span><span class="p">)</span>
    <span class="n">dtype</span> <span class="o">=</span> <span class="s2">&quot;float32&quot;</span>

    <span class="c1"># Measure build start time</span>
    <span class="n">build_start</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span>

    <span class="c1"># Start front end compilation</span>
    <span class="n">mod</span><span class="p">,</span> <span class="n">params</span> <span class="o">=</span> <span class="n">relay</span><span class="o">.</span><span class="n">frontend</span><span class="o">.</span><span class="n">from_darknet</span><span class="p">(</span><span class="n">net</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">dtype</span><span class="p">,</span> <span class="n">shape</span><span class="o">=</span><span class="n">dshape</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">target</span><span class="o">.</span><span class="n">device_name</span> <span class="o">==</span> <span class="s2">&quot;vta&quot;</span><span class="p">:</span>
        <span class="c1"># Perform quantization in Relay</span>
        <span class="c1"># Note: We set opt_level to 3 in order to fold batch norm</span>
        <span class="k">with</span> <span class="n">tvm</span><span class="o">.</span><span class="n">transform</span><span class="o">.</span><span class="n">PassContext</span><span class="p">(</span><span class="n">opt_level</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
            <span class="k">with</span> <span class="n">relay</span><span class="o">.</span><span class="n">quantize</span><span class="o">.</span><span class="n">qconfig</span><span class="p">(</span>
                <span class="n">global_scale</span><span class="o">=</span><span class="mf">23.0</span><span class="p">,</span>
                <span class="n">skip_conv_layers</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span>
                <span class="n">store_lowbit_output</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
                <span class="n">round_for_shift</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span>
            <span class="p">):</span>
                <span class="n">mod</span> <span class="o">=</span> <span class="n">relay</span><span class="o">.</span><span class="n">quantize</span><span class="o">.</span><span class="n">quantize</span><span class="p">(</span><span class="n">mod</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">)</span>
            <span class="c1"># Perform graph packing and constant folding for VTA target</span>
            <span class="n">mod</span> <span class="o">=</span> <span class="n">graph_pack</span><span class="p">(</span>
                <span class="n">mod</span><span class="p">[</span><span class="s2">&quot;main&quot;</span><span class="p">],</span>
                <span class="n">env</span><span class="o">.</span><span class="n">BATCH</span><span class="p">,</span>
                <span class="n">env</span><span class="o">.</span><span class="n">BLOCK_OUT</span><span class="p">,</span>
                <span class="n">env</span><span class="o">.</span><span class="n">WGT_WIDTH</span><span class="p">,</span>
                <span class="n">start_name</span><span class="o">=</span><span class="n">pack_dict</span><span class="p">[</span><span class="n">MODEL_NAME</span><span class="p">][</span><span class="mi">0</span><span class="p">],</span>
                <span class="n">stop_name</span><span class="o">=</span><span class="n">pack_dict</span><span class="p">[</span><span class="n">MODEL_NAME</span><span class="p">][</span><span class="mi">1</span><span class="p">],</span>
                <span class="n">start_name_idx</span><span class="o">=</span><span class="n">pack_dict</span><span class="p">[</span><span class="n">MODEL_NAME</span><span class="p">][</span><span class="mi">2</span><span class="p">],</span>
                <span class="n">stop_name_idx</span><span class="o">=</span><span class="n">pack_dict</span><span class="p">[</span><span class="n">MODEL_NAME</span><span class="p">][</span><span class="mi">3</span><span class="p">],</span>
            <span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">mod</span> <span class="o">=</span> <span class="n">mod</span><span class="p">[</span><span class="s2">&quot;main&quot;</span><span class="p">]</span>

    <span class="c1"># Compile Relay program with AlterOpLayout disabled</span>
    <span class="k">with</span> <span class="n">vta</span><span class="o">.</span><span class="n">build_config</span><span class="p">(</span><span class="n">disabled_pass</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;AlterOpLayout&quot;</span><span class="p">}):</span>
        <span class="n">lib</span> <span class="o">=</span> <span class="n">relay</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="n">mod</span><span class="p">,</span> <span class="n">target</span><span class="o">=</span><span class="n">target</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">params</span><span class="p">,</span> <span class="n">target_host</span><span class="o">=</span><span class="n">env</span><span class="o">.</span><span class="n">target_host</span><span class="p">)</span>

    <span class="c1"># Measure Relay build time</span>
    <span class="n">build_time</span> <span class="o">=</span> <span class="n">time</span><span class="o">.</span><span class="n">time</span><span class="p">()</span> <span class="o">-</span> <span class="n">build_start</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">MODEL_NAME</span> <span class="o">+</span> <span class="s2">&quot; inference graph built in </span><span class="si">{0:.2f}</span><span class="s2">s!&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">build_time</span><span class="p">))</span>

    <span class="c1"># Send the inference library over to the remote RPC server</span>
    <span class="n">temp</span> <span class="o">=</span> <span class="n">utils</span><span class="o">.</span><span class="n">tempdir</span><span class="p">()</span>
    <span class="n">lib</span><span class="o">.</span><span class="n">export_library</span><span class="p">(</span><span class="n">temp</span><span class="o">.</span><span class="n">relpath</span><span class="p">(</span><span class="s2">&quot;graphlib.tar&quot;</span><span class="p">))</span>
    <span class="n">remote</span><span class="o">.</span><span class="n">upload</span><span class="p">(</span><span class="n">temp</span><span class="o">.</span><span class="n">relpath</span><span class="p">(</span><span class="s2">&quot;graphlib.tar&quot;</span><span class="p">))</span>
    <span class="n">lib</span> <span class="o">=</span> <span class="n">remote</span><span class="o">.</span><span class="n">load_module</span><span class="p">(</span><span class="s2">&quot;graphlib.tar&quot;</span><span class="p">)</span>

    <span class="c1"># Graph executor</span>
    <span class="n">m</span> <span class="o">=</span> <span class="n">graph_executor</span><span class="o">.</span><span class="n">GraphModule</span><span class="p">(</span><span class="n">lib</span><span class="p">[</span><span class="s2">&quot;default&quot;</span><span class="p">](</span><span class="n">ctx</span><span class="p">))</span>
</pre></div>
</div>
<p class="sphx-glr-script-out">输出:</p>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span>/tvm_chinese/tvm/python/tvm/relay/build_module.py:333: DeprecationWarning: Please use input parameter mod (tvm.IRModule) instead of deprecated parameter mod (tvm.relay.function.Function)
  DeprecationWarning,
yolov3-tiny inference graph built in 15.31s!
</pre></div>
</div>
</div>
<div class="section" id="perform-image-detection-inference">
<h2>Perform image detection inference.<a class="headerlink" href="#perform-image-detection-inference" title="永久链接至标题">¶</a></h2>
<p>We run detect on an downloaded image
Download test image</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">neth</span><span class="p">,</span> <span class="n">netw</span><span class="p">]</span> <span class="o">=</span> <span class="n">dshape</span><span class="p">[</span><span class="mi">2</span><span class="p">:]</span>
<span class="n">test_image</span> <span class="o">=</span> <span class="s2">&quot;person.jpg&quot;</span>
<span class="n">img_url</span> <span class="o">=</span> <span class="n">REPO_URL</span> <span class="o">+</span> <span class="s2">&quot;data/&quot;</span> <span class="o">+</span> <span class="n">test_image</span> <span class="o">+</span> <span class="s2">&quot;?raw=true&quot;</span>
<span class="n">img_path</span> <span class="o">=</span> <span class="n">download_testdata</span><span class="p">(</span><span class="n">img_url</span><span class="p">,</span> <span class="n">test_image</span><span class="p">,</span> <span class="s2">&quot;data&quot;</span><span class="p">)</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">darknet</span><span class="o">.</span><span class="n">load_image</span><span class="p">(</span><span class="n">img_path</span><span class="p">,</span> <span class="n">neth</span><span class="p">,</span> <span class="n">netw</span><span class="p">)</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>

<span class="c1"># Prepare test image for inference</span>
<span class="n">plt</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="o">.</span><span class="n">transpose</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">,</span> <span class="p">:]</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">repeat</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">env</span><span class="o">.</span><span class="n">BATCH</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>

<span class="c1"># Set the network parameters and inputs</span>
<span class="n">m</span><span class="o">.</span><span class="n">set_input</span><span class="p">(</span><span class="s2">&quot;data&quot;</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>

<span class="c1"># Perform inference and gather execution statistics</span>
<span class="c1"># More on: :py:method:`tvm.runtime.Module.time_evaluator`</span>
<span class="n">num</span> <span class="o">=</span> <span class="mi">4</span>  <span class="c1"># number of times we run module for a single measurement</span>
<span class="n">rep</span> <span class="o">=</span> <span class="mi">3</span>  <span class="c1"># number of measurements (we derive std dev from this)</span>
<span class="n">timer</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">module</span><span class="o">.</span><span class="n">time_evaluator</span><span class="p">(</span><span class="s2">&quot;run&quot;</span><span class="p">,</span> <span class="n">ctx</span><span class="p">,</span> <span class="n">number</span><span class="o">=</span><span class="n">num</span><span class="p">,</span> <span class="n">repeat</span><span class="o">=</span><span class="n">rep</span><span class="p">)</span>

<span class="k">if</span> <span class="n">env</span><span class="o">.</span><span class="n">TARGET</span> <span class="ow">in</span> <span class="p">[</span><span class="s2">&quot;sim&quot;</span><span class="p">,</span> <span class="s2">&quot;tsim&quot;</span><span class="p">]:</span>
    <span class="n">simulator</span><span class="o">.</span><span class="n">clear_stats</span><span class="p">()</span>
    <span class="n">timer</span><span class="p">()</span>
    <span class="n">sim_stats</span> <span class="o">=</span> <span class="n">simulator</span><span class="o">.</span><span class="n">stats</span><span class="p">()</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">Execution statistics:&quot;</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">sim_stats</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
        <span class="c1"># Since we execute the workload many times, we need to normalize stats</span>
        <span class="c1"># Note that there is always one warm up run</span>
        <span class="c1"># Therefore we divide the overall stats by (num * rep + 1)</span>
        <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\t</span><span class="si">{:&lt;16}</span><span class="s2">: </span><span class="si">{:&gt;16}</span><span class="s2">&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="o">//</span> <span class="p">(</span><span class="n">num</span> <span class="o">*</span> <span class="n">rep</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)))</span>
<span class="k">else</span><span class="p">:</span>
    <span class="n">tcost</span> <span class="o">=</span> <span class="n">timer</span><span class="p">()</span>
    <span class="n">std</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">tcost</span><span class="o">.</span><span class="n">results</span><span class="p">)</span> <span class="o">*</span> <span class="mi">1000</span>
    <span class="n">mean</span> <span class="o">=</span> <span class="n">tcost</span><span class="o">.</span><span class="n">mean</span> <span class="o">*</span> <span class="mi">1000</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\n</span><span class="s2">Performed inference in </span><span class="si">%.2f</span><span class="s2">ms (std = </span><span class="si">%.2f</span><span class="s2">) for </span><span class="si">%d</span><span class="s2"> samples&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">mean</span><span class="p">,</span> <span class="n">std</span><span class="p">,</span> <span class="n">env</span><span class="o">.</span><span class="n">BATCH</span><span class="p">))</span>
    <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Average per sample inference time: </span><span class="si">%.2f</span><span class="s2">ms&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">mean</span> <span class="o">/</span> <span class="n">env</span><span class="o">.</span><span class="n">BATCH</span><span class="p">))</span>

<span class="c1"># Get detection results from out</span>
<span class="n">thresh</span> <span class="o">=</span> <span class="mf">0.5</span>
<span class="n">nms_thresh</span> <span class="o">=</span> <span class="mf">0.45</span>
<span class="n">tvm_out</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">):</span>
    <span class="n">layer_out</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">layer_out</span><span class="p">[</span><span class="s2">&quot;type&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;Yolo&quot;</span>
    <span class="c1"># Get the yolo layer attributes (n, out_c, out_h, out_w, classes, total)</span>
    <span class="n">layer_attr</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">get_output</span><span class="p">(</span><span class="n">i</span> <span class="o">*</span> <span class="mi">4</span> <span class="o">+</span> <span class="mi">3</span><span class="p">)</span><span class="o">.</span><span class="n">numpy</span><span class="p">()</span>
    <span class="n">layer_out</span><span class="p">[</span><span class="s2">&quot;biases&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">get_output</span><span class="p">(</span><span class="n">i</span> <span class="o">*</span> <span class="mi">4</span> <span class="o">+</span> <span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">numpy</span><span class="p">()</span>
    <span class="n">layer_out</span><span class="p">[</span><span class="s2">&quot;mask&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">get_output</span><span class="p">(</span><span class="n">i</span> <span class="o">*</span> <span class="mi">4</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">numpy</span><span class="p">()</span>
    <span class="n">out_shape</span> <span class="o">=</span> <span class="p">(</span><span class="n">layer_attr</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">layer_attr</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">//</span> <span class="n">layer_attr</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">layer_attr</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">layer_attr</span><span class="p">[</span><span class="mi">3</span><span class="p">])</span>
    <span class="n">layer_out</span><span class="p">[</span><span class="s2">&quot;output&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">get_output</span><span class="p">(</span><span class="n">i</span> <span class="o">*</span> <span class="mi">4</span><span class="p">)</span><span class="o">.</span><span class="n">numpy</span><span class="p">()</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="n">out_shape</span><span class="p">)</span>
    <span class="n">layer_out</span><span class="p">[</span><span class="s2">&quot;classes&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">layer_attr</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span>
    <span class="n">tvm_out</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">layer_out</span><span class="p">)</span>
    <span class="n">thresh</span> <span class="o">=</span> <span class="mf">0.560</span>

<span class="c1"># Show detection results</span>
<span class="n">img</span> <span class="o">=</span> <span class="n">darknet</span><span class="o">.</span><span class="n">load_image_color</span><span class="p">(</span><span class="n">img_path</span><span class="p">)</span>
<span class="n">_</span><span class="p">,</span> <span class="n">im_h</span><span class="p">,</span> <span class="n">im_w</span> <span class="o">=</span> <span class="n">img</span><span class="o">.</span><span class="n">shape</span>
<span class="n">dets</span> <span class="o">=</span> <span class="n">yolo_detection</span><span class="o">.</span><span class="n">fill_network_boxes</span><span class="p">((</span><span class="n">netw</span><span class="p">,</span> <span class="n">neth</span><span class="p">),</span> <span class="p">(</span><span class="n">im_w</span><span class="p">,</span> <span class="n">im_h</span><span class="p">),</span> <span class="n">thresh</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">tvm_out</span><span class="p">)</span>
<span class="n">last_layer</span> <span class="o">=</span> <span class="n">net</span><span class="o">.</span><span class="n">layers</span><span class="p">[</span><span class="n">net</span><span class="o">.</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span>
<span class="n">yolo_detection</span><span class="o">.</span><span class="n">do_nms_sort</span><span class="p">(</span><span class="n">dets</span><span class="p">,</span> <span class="n">last_layer</span><span class="o">.</span><span class="n">classes</span><span class="p">,</span> <span class="n">nms_thresh</span><span class="p">)</span>
<span class="n">yolo_detection</span><span class="o">.</span><span class="n">draw_detections</span><span class="p">(</span><span class="n">font_path</span><span class="p">,</span> <span class="n">img</span><span class="p">,</span> <span class="n">dets</span><span class="p">,</span> <span class="n">thresh</span><span class="p">,</span> <span class="n">names</span><span class="p">,</span> <span class="n">last_layer</span><span class="o">.</span><span class="n">classes</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">imshow</span><span class="p">(</span><span class="n">img</span><span class="o">.</span><span class="n">transpose</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">))</span>
<span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
</pre></div>
</div>
<img alt="../../../../_images/sphx_glr_deploy_detection_001.png" class="sphx-glr-single-img" src="../../../../_images/sphx_glr_deploy_detection_001.png" />
<p class="sphx-glr-script-out">输出:</p>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span>Execution statistics:
        inp_load_nbytes :         25462784
        wgt_load_nbytes :         17558016
        acc_load_nbytes :            96128
        uop_load_nbytes :             5120
        out_store_nbytes:          3396224
        gemm_counter    :         10578048
        alu_counter     :          1061320
</pre></div>
</div>
<div class="sphx-glr-footer class sphx-glr-footer-example docutils container" id="sphx-glr-download-topic-vta-tutorials-frontend-deploy-detection-py">
<div class="sphx-glr-download docutils container">
<p><a class="reference download internal" download="" href="../../../../_downloads/65b9451c8de050d7cd9da2fe5a49acc6/deploy_detection.py"><code class="xref download docutils literal notranslate"><span class="pre">Python</span> <span class="pre">源码下载:</span> <span class="pre">deploy_detection.py</span></code></a></p>
</div>
<div class="sphx-glr-download docutils container">
<p><a class="reference download internal" download="" href="../../../../_downloads/66e1a42229aae7ed49ac268f520e6727/deploy_detection.ipynb"><code class="xref download docutils literal notranslate"><span class="pre">Jupyter</span> <span class="pre">notebook</span> <span class="pre">下载:</span> <span class="pre">deploy_detection.ipynb</span></code></a></p>
</div>
</div>
<p class="sphx-glr-signature"><a class="reference external" href="https://sphinx-gallery.github.io">Gallery generated by Sphinx-Gallery</a></p>
</div>
</div>


           </div>
           
          </div>
          

<footer>

    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="../optimize/convolution_opt.html" class="btn btn-neutral float-right" title="2D Convolution Optimization" accesskey="n" rel="next">下一个 <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="deploy_classification.html" class="btn btn-neutral float-left" title="Deploy Pretrained Vision Model from MxNet on VTA" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> 上一个</a>
      
    </div>

<div id="button" class="backtop"><img src="../../../../_static//img/right.svg" alt="backtop"/> </div>
<section class="footerSec">
    <div class="footerHeader">
      <ul class="d-flex align-md-items-center justify-content-between flex-column flex-md-row">
        <li class="copywrite d-flex align-items-center">
          <h5 id="copy-right-info">© 2020 Apache Software Foundation | All right reserved</h5>
        </li>
      </ul>

    </div>

    <ul>
      <li class="footernote">Copyright © 2020 The Apache Software Foundation. Apache TVM, Apache, the Apache feather, and the Apache TVM project logo are either trademarks or registered trademarks of the Apache Software Foundation.</li>
    </ul>

</section>
</footer>
        </div>
      </div>

    </section>

  </div>
  

    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

  </body>
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
    <!-- Theme Analytics -->
    <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-75982049-2', 'auto');
    ga('send', 'pageview');
    </script>

    
   

</body>
</html>